package com.itheima.controller;

import com.itheima.pojo.StatusCode;
import com.itheima.pojo.UserVO;
import com.itheima.pojo.PageResult;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/getList")
    public Map<String, Object> getList(@RequestBody UserVO userVO){
        PageResult list = userService.getList(userVO);
        List rows = list.getRows();
        Long total = list.getTotal();

        Map<String,Object> result = new HashMap<>();

        Map<String,Object> recordsMap = new HashMap<>();
        recordsMap.put("records",rows);
        recordsMap.put("total",total);
        recordsMap.put("size",userVO.getPageSize());
        recordsMap.put("current",userVO.getCurrentPage());
        recordsMap.put("orders","");
        recordsMap.put("optimizeCountSql",true);
        recordsMap.put("isSearchCount",true);

        result.put("code","success");
        result.put("data",recordsMap);
        return result;

    }

    @PostMapping("/delete")
    public Map delete(@RequestBody UserVO userVO){

        String uid = userVO.getUid();

        int i = userService.delete(uid);
        Map<String,Object> map = new HashMap<>();
        if (i==0){
            map.put("success",i);
            map.put("resultCode",00001);
            map.put("message","操作失败");
            return map;
        }else {
            map.put("success",i);
            map.put("resultCode",00002);
            map.put("message","操作成功");
            return map;
        }
    }


    @PostMapping("/edit")
    public Map edit(@RequestBody UserVO userVO){
        int edit = userService.edit(userVO);
        Map<String,Object> map = new HashMap<>();
        if(edit==0){
            map.put("success",edit);
            map.put("resultCode",00001);
            map.put("message","操作失败");
            return map;
        }else {
            map.put("code","success");
            map.put("resultCode",null);
            map.put("message","操作成功");
            map.put("data","编辑成功");
            map.put("success",null);
            return map;
        }
    }

    @PostMapping("/resetUserPassword")
    public Map resetUserPassword(@RequestBody UserVO userVO){
        userService.resetUserPassword(userVO);

        Map <String,Object> map = new HashMap<>();
        map.put("success",1);   //00002
        map.put("resultCode", StatusCode.LOGINERROR);
        map.put("message","操作成功");
        return map ;
    }


}
